<template>
    <div>
        <h1>{{id?`编辑`:`新建`}}论坛</h1>
        <el-form label-width='120px'>
            <el-form-item label="论坛标题"> 
                <el-input v-model="model.name" clearable></el-input>
            </el-form-item>
           
            <el-form-item label="发布者"> 
                <el-input v-model="model.lowzhu" clearable></el-input>
            </el-form-item>
             <el-form-item label="浏览量"> 
                <el-input v-model="model.look" clearable></el-input>
            </el-form-item>
            <el-form-item label="发布时间"> 
                <el-input v-model="model.time" clearable></el-input>
            </el-form-item>
            <el-form-item label="论坛内容"> 
                <el-input type="textarea" v-model="model.content" clearable></el-input>
            </el-form-item>
            <el-form-item label="论坛图片"> 
               <el-upload 
                class="avatar-uploader"
                :action="uploadURL"
                :headers="getAuthHeaders()"
                :show-file-list="false"
                :on-success="afterUpload"
                >
                     <!-- 有图片显示图片，没有则显示上传图标,:src显示的图片 -->
                    <img v-if="model.img" :src="model.img" class="avatar"/>
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
               </el-upload>
            </el-form-item>
            
            <el-form-item>
                <el-button type='primary' @click="save()" native-type='submit'>
                    保存
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    props:{
        id:{}
    },
    data(){
        return{
            model:{},
        }
    },
    methods:{
        async save(){
            let res
            if(this.id){
                res = await this.$http.put(`rest/Forum/${this.id}`,this.model)
            }else{
                res = await this.$http.post('rest/Forum',this.model)
            }
            this.$router.push('/forum/list')
            this.$message({
                type:'success',
                message:'保存成功'
            })
        },
        async fetch(){
            const res = await this.$http.get('rest/Forum/'+this.id)
            this.model = res.data
        },
        async afterUpload(res){
            // 新增监听数据变化
            this.$set(this.model,'img',res.url)
            // 变化了无法监听
            // this.model.icon = res.url
        }
        
    },
    created(){
        // this.fetchParent()
        if(this.id){
            this.fetch()
        }
    }
}
</script>



<style scoped>

</style>